package com.fangcun.service.admin.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.admin.message.response.MessageDataResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.entity.MessageData;
import com.fangcun.entity.User;
import com.fangcun.mapper.MessageDataMapper;
import com.fangcun.mapper.UserMapper;
import com.fangcun.service.admin.MessageDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class MessageDataServiceImpl implements MessageDataService {

	@Autowired
	private MessageDataMapper messageDataMapper;
	@Autowired
	private UserMapper userMapper;

	/****/
	@Override
	public PageInfo<MessageDataResponse> list(PageBean<MessageData> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<MessageDataResponse> list = messageDataMapper.selectAll(request.getParam());
		for (MessageDataResponse messageDataResponse : list) {
			if (messageDataResponse.getSendUserId() == 0) {
				messageDataResponse.setAName("系统消息");
			}
			if (messageDataResponse.getReceiveUserId() == 0) {
				messageDataResponse.setBName("全部");
			}
		}
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	/** 发送失败 单个用户 或者 多个用户 **/
	@Override
	public void send(List<MessageData> request) throws ProException {
		for (MessageData messageData : request) {
			if (messageDataMapper.insertSelective(messageData) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}

	/** 下拉 **/
	@Override
	public List<User> dropDown(User request) throws ProException {
		List<User> list = userMapper.selectUser(request);
		if (list == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}
}
